package com.mindskip.xzs.controller.admin;

import com.github.pagehelper.PageInfo;
import com.mindskip.xzs.base.RestResponse;
import com.mindskip.xzs.domain.User;
import com.mindskip.xzs.service.NewUserService;
import com.mindskip.xzs.viewmodel.IdNameKV;
import com.mindskip.xzs.viewmodel.admin.user.EmployeeReq;
import com.mindskip.xzs.viewmodel.admin.user.EmployeeRes;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author artka
 * @description:
 * @date 2022/9/16 10:58 PM
 */
@RestController("EmployeeController")
@RequestMapping(value = "/api/admin/employee")
public class EmployeeController {

    @Resource
    private NewUserService newUserService;

    /**
     * 分页获取所有员工
     * 适配前端，param传参
     */
    @GetMapping("/list")
    public RestResponse<PageInfo<EmployeeRes>> listEmpList(@RequestParam(required = false, value = "pageNum", defaultValue = "1") Integer pageNum,
                                                           @RequestParam(required = false, value = "pageSize", defaultValue = "10") Integer pageSize,
                                                           @RequestParam(required = false, value = "userName") String userName,
                                                           @RequestParam(required = false, value = "status") Integer status,
                                                           @RequestParam(required = false, value = "deptId") Long deptId,
                                                           @RequestParam(required = false, value = "beginTime") String beginTime,
                                                           @RequestParam(required = false, value = "endTime") String endTime) {
        return RestResponse.ok(newUserService.queryUserList(
                new EmployeeReq()
                        .setPageNum(pageNum)
                        .setPageSize(pageSize)
                        .setUserName(userName)
                        .setStatus(status)
                        .setDeptId(deptId)
                        .setBeginTime(beginTime)
                        .setEndTime(endTime)
        ));
    }

    @PostMapping("/exportList")
    public void exportList(@RequestBody(required = false) EmployeeReq req, HttpServletResponse response) {
        newUserService.exportUserList(
                req.setPageNum(1).setPageSize(100000),
                response
        );
    }

    @GetMapping("/updateStatus")
    public RestResponse<Boolean> updateEmploy(@RequestParam Integer id, @RequestParam Integer status) {
        return RestResponse.ok(newUserService.updateById(new User().setId(id).setStatus(status)));
    }

    /**
     * 查询员工kv列表
     */
    @RequestMapping(value = "/list/kv", method = RequestMethod.GET)
    public RestResponse<List<IdNameKV>> selectCategory(@RequestParam(required = false, value = "keyword") String keyword) {
        return RestResponse.ok(newUserService.queryUserKVListInCache(keyword));
    }
}
